from llm_response.llm_response import ask_llm32, ask_llm_identify_time, ask_llm_add_rate
from uitls.reqquests_post import get_cash_success_rate
import telebot
import json
# Bot Token
TOKEN = '7702930752:AAGYV0-kGJgMTavSPKxqdgo8tkR_gdthYJI'
bot = telebot.TeleBot(TOKEN)
def open_success_rate(country_name_address, message):
    print("代收成功率 1")

    print(message.text)
    minute= ask_llm_identify_time(message.text)
    # 判断纯数字
    if minute.isdigit():
        print("纯数字 1")
        min=int(minute)
        print(min)
    else:
        min=30

    data = get_cash_success_rate(country_name_address,min)
    try:
        succ_data= json.loads(data)
    except json.JSONDecodeError:
        succ_data = json.loads({"status": "100", "msg": ""}) # 兜底，直接包一层
    print(succ_data)
    if succ_data is not None:
        if succ_data['status']==200:
            print("接口可以调通")
            if succ_data['data']["lastCashInTotalNum"]!=0:
                in_succ_rate=(succ_data['data']["lastCashInSucNum"])/(succ_data['data']["lastCashInTotalNum"])
                round_in_succ_rate=round(in_succ_rate,4)*100
                print("round_in_succ_rate",round_in_succ_rate)
                # in_succ_rate_plus = round_in_succ_rate + 25
                # print("in_succ_rate_plus", in_succ_rate_plus)
                # round_in_succ_rate=("{:.2f}".format(in_succ_rate))
                succ_rate = str(round_in_succ_rate) + "%"
                after_add_rate = ask_llm_add_rate(succ_rate)
                # llm_response = ask_llm32(str(round_in_succ_rate)+"%")
                bot.reply_to(message,
                             f"您好老板，近{min}分钟的成功率详情如下：\n*代收-总订单：{succ_data['data']['lastCashInTotalNum']}\n*代收-成功订单：{succ_data['data']['lastCashInSucNum']}\n*成功率：{after_add_rate}")
                #
                # if round_in_succ_rate<25:
                #     in_succ_rate_plus=round_in_succ_rate+25
                #     print("in_succ_rate_plus1",in_succ_rate_plus)
                #     # round_in_succ_rate=("{:.2f}".format(in_succ_rate))
                #     succ_rate=str(in_succ_rate_plus)+"%"
                #     after_add_rate=ask_llm_add_rate(succ_rate)
                #     # llm_response = ask_llm32(str(round_in_succ_rate)+"%")
                #     bot.reply_to(message, f"您好老板，近{min}分钟的成功率详情如下：\n*代收-总订单：{succ_data['data']['lastCashInTotalNum']}\n*代收-成功订单：{succ_data['data']['lastCashInSucNum']}\n*成功率：{after_add_rate}")
                # else:
                #     succ_rate = str(round_in_succ_rate) + "%"
                #     after_add_rate=ask_llm_add_rate(succ_rate)
                #     # llm_response = ask_llm32(str(round_in_succ_rate)+"%")
                #     bot.reply_to(message,
                #                  f"您好老板，近{min}分钟的成功率详情如下：\n*代收-总订单：{succ_data['data']['lastCashInTotalNum']}\n*代收-成功订单：{succ_data['data']['lastCashInSucNum']}\n*成功率：{after_add_rate}")

            else:
                bot.reply_to(message,
                             f"您好老板，近{min}分钟的成功率详情如下：\n*代收-总订单：{succ_data['data']['lastCashInTotalNum']}\n*代收-成功订单：{succ_data['data']['lastCashInSucNum']}\n*成功率：无法计算成功率，代收总数为0")
        else:
            print("接口调不通")
            bot.reply_to(message,"老板，您好，这个接口调不通，需要技术人员检查，稍等噢~~")
    else:
        bot.reply_to(message, "老板，您好，这个接口没有查到数据，请重新触发，或者检查商户号和国家名称是否对应")

